import { setToken } from '@/utils/auth';
import {
  register, loginPassword, loginVerifycode, resetPassword,
} from '@/api/passport';
import { Message } from 'element-ui';
import store from '@/store/index';

export default {
  namespaced: true,
  state: {
    nowAction: 'login',
    isVerifycodeLogin: false,
    sign: {
      value: '',
      tip: '',
      state: false,
      signType: null,
    },
    nickname: {
      value: '',
      tip: '',
      state: false,
    },
    password: {
      value: '',
      tip: '',
      state: false,
    },
    verifycode: {
      value: '',
      tip: '',
      state: false,
      sending: false,
      expiresIn: 0,
    },
  },
  mutations: {
    TOOGEL_NOW_ACTION: (state, data) => {
      state.nowAction = data;
    },
    TOOGEL_IS_VERIFYCODE_LOGIN: (state, data) => {
      state.isVerifycodeLogin = data;
    },
  },
  actions: {
    async loginHandler({ state }) {
      const { sign, isVerifycodeLogin } = state;
      const { signType } = sign;
      const options = {};
      options[signType] = sign.value;
      const loginType = isVerifycodeLogin ? 'verifycode' : 'password';
      options[loginType] = state[loginType].value;
      const funObj = {
        password: loginPassword,
        verifycode: loginVerifycode,
      };
      if (options[signType].trim() === '') {
        sign.tip = '帐号不能为空';
        sign.state = false;
        return;
      }
      if (!signType) {
        sign.tip = '邮箱/手机号格式不正确';
        sign.state = false;
        return;
      }
      if (options[loginType].trim() === '') {
        state[loginType].tip = isVerifycodeLogin ? '验证码不能为空' : '密码不能为空';
        state[loginType].state = false;
        return;
      }
      const { code, data: token, message } = await funObj[loginType](options);
      Message({
        message,
        type: code === 200 ? 'success' : 'error',
        duration: 1000,
        onClose: () => {
          // 判断登录成功,在cookie里保存token
          if (token) {
            setToken(token);
            // 保存成功后页面跳转
            store.dispatch('verifyLogin');
          }
        },
      });
      if (code === 404) {
        state.nowAction = 'register';
        state.verifycode.value = '';
      }
    },
    async registerHandler({ commit, state }) {
      const {
        nickname, password, verifycode, sign,
      } = state;
      const { signType } = sign;
      const options = {
        nickname: nickname.value,
        password: password.value,
        verifycode: verifycode.value,
      };
      options[signType] = sign.value;
      if (String(options.nickname).trim() === '') {
        nickname.tip = '昵称不能为空';
        nickname.state = false;
        return;
      }
      if (String(options[signType]).trim() === '') {
        sign.tip = '邮箱/手机号不能为空';
        sign.state = false;
        return;
      }
      if (!signType) {
        sign.tip = '邮箱/手机号格式不正确';
        sign.state = false;
        return;
      }
      if (String(options.password).trim() === '') {
        password.tip = '密码不能为空';
        password.state = false;
        return;
      }
      if (String(options.verifycode).trim().length !== 6) {
        verifycode.tip = '验证码格式不正确';
        verifycode.state = false;
        return;
      }
      const { code, message } = await register(options);
      Message({
        message,
        type: code === 200 ? 'success' : 'error',
      });
      if (code === 200) {
        state.nowAction = 'login';
        state.verifycode.value = '';
        commit('TOOGEL_IS_VERIFYCODE_LOGIN', false);
      }
    },

    async resetPassword({ commit, state }) {
      const { password, verifycode, sign } = state;
      const { signType } = sign;
      const options = {
        password: password.value,
        verifycode: verifycode.value,
      };
      options[signType] = sign.value;
      if (String(options[signType]).trim() === '') {
        sign.tip = '邮箱/手机号不能为空';
        sign.state = false;
        return;
      }
      if (!signType) {
        sign.tip = '邮箱/手机号格式不正确';
        sign.state = false;
        return;
      }
      if (String(options.password).trim() === '') {
        password.tip = '密码不能为空';
        password.state = false;
        return;
      }
      if (String(options.verifycode).trim().length !== 6) {
        verifycode.tip = '验证码格式不正确';
        verifycode.state = false;
        return;
      }
      const { code, message } = await resetPassword(options);
      Message({
        message,
        type: code === 200 ? 'success' : 'error',
      });
      if (code === 200) {
        state.nowAction = 'login';
        state.verifycode.value = '';
        store.dispatch('page/toogelPage');
        commit('TOOGEL_IS_VERIFYCODE_LOGIN', false);
      }
    },
  },
};
